package com.shuqing.download.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 数据库配置
 * @author wangshuqing
 */
@Slf4j
public class DruidCommonConfig {

    @Autowired
    private DruidCommonParam druidCommonParam;

    /**
    * 数据源设置其它属性
    * @date 2023/2/25 16:33
    * @author wangshuqing
    * @param datasource
    * */
    protected void dataSourceSetParam(DruidDataSource datasource) {
        datasource.setDriverClassName(druidCommonParam.getDriverClassName());
        datasource.setInitialSize(druidCommonParam.getInitialSize());
        datasource.setMinIdle(druidCommonParam.getMinIdle());
        datasource.setMaxActive(druidCommonParam.getMaxActive());
        datasource.setMaxWait(druidCommonParam.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(druidCommonParam.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(druidCommonParam.getMinEvictableIdleTimeMillis());
        datasource.setMaxEvictableIdleTimeMillis(druidCommonParam.getMaxEvictableIdleTimeMillis());
        datasource.setValidationQuery(druidCommonParam.getValidationQuery());
        datasource.setTestWhileIdle(druidCommonParam.isTestWhileIdle());
        datasource.setTestOnBorrow(druidCommonParam.isTestOnBorrow());
        datasource.setTestOnReturn(druidCommonParam.isTestOnReturn());
        datasource.setPoolPreparedStatements(druidCommonParam.isPoolPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(druidCommonParam.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            datasource.setFilters(druidCommonParam.getFilters());
        } catch (Exception e) {
            log.error("druid configuration initialization filter", e);
        }
        datasource.setConnectionProperties(druidCommonParam.getConnectionProperties());
    }

}